package code.generate.util;

import code.generate.GenMojo;
import org.apache.commons.lang3.StringUtils;

import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.*;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 2019/3/23 13:38
 */
public class JDBCUtil {
    private static String driver = "com.mysql.jdbc.Driver";

    private static String url;

    private static String user;

    private static String password;

    private static Connection conn;

    private JDBCUtil() {
    }

    static {
        try {
            /**
             * 使用properties集合读取配置信息
             */
            FileReader fr = new FileReader(GenMojo.CODE_DIR);
            Properties p = new Properties();
            p.load(fr);

            if (StringUtils.isNotEmpty(p.getProperty("driver")))
                driver = p.getProperty("driver");
            url = p.getProperty("url");
            user = p.getProperty("user");
            password = p.getProperty("password");

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库连接失败！");
        }
    }

    public static Connection getConnection() {
        return conn;
    }

    /**
     * 定义一个静态方法,用于释放资源
     */
    public static void close(ResultSet rs, Statement stat, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static <T> List<T> query(String sql) {
        ResultSet resultSet = null;
        Statement statement = null;
        Connection connection = null;
        List list = null;
        try {
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            list = convertList(resultSet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(resultSet, statement, connection);
        }


        return list;
    }

    public static int update(String sql) {
        Statement statement = null;
        Connection connection = null;
        try {
            connection = getConnection();
            statement = connection.createStatement();
            return statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(null, statement, connection);
        }


        return 0;
    }

    private static List convertList(ResultSet rs) throws SQLException {
        List list = new ArrayList();
        ResultSetMetaData md = rs.getMetaData();//获取键名
        int columnCount = md.getColumnCount();//获取行的数量
        while (rs.next()) {
            Map rowData = new HashMap();//声明Map
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值
            }
            list.add(rowData);
        }
        return list;
    }


}
